package one.stand.controller;

import com.jmp.dto.TenxunImReceiveRequest;
import com.jmp.dto.TenxunImReceiveResponse;
import com.jmp.im.ImNotifyClient;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import one.stand.ReceiveMsgActionService;
import one.stand.util.OneListUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;

@Api(description = "会话消息回调")
@RestController
@Slf4j
public class ReceiveMsgController {
    @Autowired
    private ImNotifyClient imNotifyClient;
    @Autowired
    private ReceiveMsgActionService receiveMsgActionService;

    /**
     * 云信回调
     */
//    @PostMapping("/receiveMsg.action")
//    public void receiveMsg(@RequestBody NotifyReceiveMsgRequest receiveMsgRequest, HttpServletRequest request, HttpServletResponse response) {
    public void receiveMsg(HttpServletRequest request, HttpServletResponse response) {
        try {
            BufferedReader br = request.getReader();
            String str;
            StringBuilder wholeStr = new StringBuilder();
            while ((str = br.readLine()) != null) {
                wholeStr.append(str);
            }
            log.info("{}", wholeStr.toString());
            receiveMsgActionService.execute(wholeStr.toString(), request);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            imNotifyClient.success(response);
        }
    }

    /**
     * 腾讯IM回调
     */
    @PostMapping("/tenxun/im/receive")
    public TenxunImReceiveResponse tenxunImReceive(@RequestParam("SdkAppid") String SdkAppid,
                                                   @RequestParam("CallbackCommand") String CallbackCommand,
                                                   @RequestParam("OptPlatform") String OptPlatform,
                                                   @RequestBody TenxunImReceiveRequest request) {

        if (!"1400360457".equals(SdkAppid)) {
            log.info("SdkAppid is {}, return", SdkAppid);
            return TenxunImReceiveResponse.fail(null);
        }
        if ("C2C.CallbackAfterSendMsg".equals(CallbackCommand)) {
            String content = StringUtils.EMPTY;
            if (OneListUtil.isNotEmpty(request.getMsgBody())) {
                TenxunImReceiveRequest.Msg msg = request.getMsgBody().get(0);
                content = msg.getMsgContent().getText();
            }
            receiveMsgActionService.execute(request.getFrom_Account(), request.getTo_Account(), content);
            return TenxunImReceiveResponse.success();
        } else if ("State.StateChange".equals((CallbackCommand))) {
            receiveMsgActionService.loginStatus(request.getInfo().getTo_Account(), request.getInfo().isLogin(), isApp(OptPlatform));
            return TenxunImReceiveResponse.success();
        }

        log.info("CallbackCommand is {}, return", CallbackCommand);
        return TenxunImReceiveResponse.success();
    }

    private boolean isApp(String optPlatform) {
        return StringUtils.isNoneBlank(optPlatform) && ("Android".equals(optPlatform) || "iOS".equals(optPlatform));
    }
}
